package com.vn.toaa.karaoke.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@SuppressLint("SdCardPath")
public class ExternalSQLiteHelper extends SQLiteOpenHelper {
	
//	public static final String DATABASE_NAME = "readstory.sqlite";
//	public static final String DATABASE_PATH = "/data/data/com.nhn.storyreading/databases/";
//	public static final int DATABASE_VERSION = 1;
	
	private SQLiteDatabase mDatabase;
	private Context mContext;
	
	private static String sDatabaseName;
	private static String sDatabasePath;
	private static int sDatabaseVersion;
	
	private static ExternalSQLiteHelper sInstance = null;
	
	public static void createInstance(Context context, String databaseName, String databasePath, int databaseVersion) {
		if (sInstance == null) {
			sInstance = new ExternalSQLiteHelper(context, databaseName, databasePath, databaseVersion);
		}
	}
	
	public static ExternalSQLiteHelper getInstance() {
		return sInstance;
	}
	
	public ExternalSQLiteHelper(Context context, String databaseName, String databasePath, int databaseVersion) {
		super(context, databaseName, null, databaseVersion);
		mContext = context;
		sDatabaseName = databaseName;
		sDatabasePath = databasePath;
		sDatabaseVersion = databaseVersion;
	}

	public ExternalSQLiteHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	
	public void createDatabase() throws IOException {

		boolean dbExist = checkDataBase();
		if (dbExist) {
			Log.v("DB Exists", "db exists");
			// By calling this method here onUpgrade will be called on a
			// writeable database, but only if the version number has been
			// bumped
			// onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
		}

		boolean dbExist1 = checkDataBase();
		if (!dbExist1) {
			this.getReadableDatabase();
			try {
				this.close();
				copyDataBase();
			} catch (IOException e) {
				throw new Error("Error copying database");
			}
		}

	}

	// Check database already exist or not
	public boolean checkDataBase() {
		boolean checkDB = false;
		try {
			String myPath = sDatabasePath + sDatabaseName;
			File dbfile = new File(myPath);
			checkDB = dbfile.exists();
		} catch (SQLiteException e) {
		}
		return checkDB;
	}

	// Copies your database from your local assets-folder to the just created
	// empty database in the system folder
	private void copyDataBase() throws IOException {

		String outFileName = sDatabasePath + sDatabaseName;

		OutputStream myOutput = new FileOutputStream(outFileName);
		InputStream myInput = mContext.getAssets().open(sDatabaseName);

		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		myInput.close();
		myOutput.flush();
		myOutput.close();
	}

	// delete database
	public void deleteDatabase() {
		File file = new File(sDatabasePath + sDatabaseName);
		if (file.exists()) {
			file.delete();
			System.out.println("delete database file.");
		}
	}

	// Open database
	public void openDatabase() throws SQLException {
		String myPath = sDatabasePath + sDatabaseName;
		if (mDatabase == null || !mDatabase.isOpen()) {
			mDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
		}
	}

	public synchronized void closeDataBase() throws SQLException {
		if (mDatabase != null && mDatabase.isOpen())
			mDatabase.close();
		super.close();
	}
	
	public SQLiteDatabase getDatabase() {
		return mDatabase;
	}

}
